package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//套餐模块
@RestController     //@ResponseBody + @Controller
public class SetmealController {

    //依赖注入service层
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐列表 + 模糊查询
     * Request URL: http://localhost:8080/setmeal/page?page=1&pageSize=10&name=a
     * Request Method: GET
     */
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(name = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
                                 String name){      //接收RESTful风格和普通类型参数
        //1. 调用service层
        Page<Setmeal> page = setmealService.findByPage(pageNum,pageSize,name);
        //2. 返回响应
        return ResultInfo.success(page);

    }

    /**
     * 回显菜品信息 + 菜品名模糊查询
     * 查询dish 菜品表
     * Request URL: http://localhost:8080/dish/list?categoryId=1397844263642378242
     * Request URL: http://localhost:8080/dish/list?name=%E9%B8%A1
     * Request Method: GET
     */
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryIdOrName(Long categoryId,String name){        //接收普通参数
        //1. 调用service层
        List<Dish> list = setmealService.findByCategoryIdOrName(categoryId,name);
        //2. 返回响应
        return ResultInfo.success(list);
    }



    /**
     * 新增套餐信息
     * Request URL: http://localhost:8080/setmeal
     * Request Method: POST
     */
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){   //接收在请求体中的参数
        //1. 调用service层
        setmealService.save(setmeal);
        //2. 返回响应
        return ResultInfo.success();

    }

    /**
     * 回显套餐信息
     * Request URL: http://localhost:8080/setmeal/1539891755485896706
     * Request Method: GET
     */
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable Long id){  //接收RESTful风格的参数
        //1. 调用service层
        Setmeal setmeal = setmealService.findById(id);
        //2. 返回响应
        return ResultInfo.success(setmeal);
    }


    /**
     * 修改套餐信息
     * Request URL: http://localhost:8080/setmeal
     * Request Method: PUT
     */
    @PutMapping("/setmeal")
    public ResultInfo updateById(@RequestBody Setmeal setmeal){     //接收在请求体的参数
        //1. 调用service层
        setmealService.updateById(setmeal);
        //2. 返回响应
        return ResultInfo.success();
    }


    /**
     * (批量)停售、起售
     * Request URL: http://localhost:8080/setmeal/status/0?ids=1415580119015145474,1539891755485896706
     * Request Method: POST
     */
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo setStatus(@PathVariable Integer status,
                                @RequestParam List<Long> ids){  //接收RESTful风格和普通类型参数
        //1. 调用service层
        setmealService.setStatus(status,ids);
        //2. 返回响应
        return ResultInfo.success();
    }


    /**
     * (批量)删除
     * Request URL: http://localhost:8080/setmeal?ids=1415580119015145474,1539891755485896706
     * Request Method: DELETE
     */
    @DeleteMapping("setmeal")
    public ResultInfo deleteByIds(@RequestParam List<Long> ids){
        //1. 调用service层
        setmealService.deleteByIds(ids);
        //2. 返回响应
        return ResultInfo.success();
    }

}
